﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TournamentDatabase.Objects;
using TournamentDatabase.DialogBoxes.Edit;
using TournamentDatabase.Database;
using TournamentDatabase.Singletons;

namespace TournamentDatabase.Views
{
	/// <summary>
	/// Interaction logic for MemberView.xaml
	/// </summary>
	public partial class MemberView : Window
	{
		Member m_member;
		Competitor m_competitor;
		Judge m_judge;
		Organiser m_organiser;
		int count = 0;

		public Member Member
		{
			get { return m_member; }
			set { m_member = value; }
		}
		public Competitor Competitor
		{
			get { return m_competitor; }
			set { m_competitor = value; }
		}
		public Judge Judge
		{
			get { return m_judge; }
			set { m_judge = value; }
		}
		public Organiser Organiser
		{
			get { return m_organiser; }
			set { m_organiser = value; }
		}

		public MemberView(Member member)
		{
			InitializeComponent();

			m_member = member;

			LoadGens();

			this.DataContext = this;
		}

		private void LoadGens()
		{
			string where = "mid = " + m_member.MemberID;

			// Get Competitor from database
			Competitor comp = null;
			List<Competitor> comps = DatabaseSingleton.GetDatabaseAccessor().QueryCompetitor(where, OrderBy.None, "");
			if (comps.Count == 1)
				comp = comps[0];

			if (comp == null)
				competitorTabItem.IsEnabled = false;
			else
			{
				makeCompetitor.IsEnabled = false;
				makeCompetitor.Visibility = Visibility.Collapsed;
				m_competitor = comp;
			}

			// Get Judge from database
			Judge judge = null;
			List<Judge> judges = DatabaseSingleton.GetDatabaseAccessor().QueryJudge(where, OrderBy.None, "");
			if (judges.Count == 1)
				judge = judges[0];

			if (judge == null)
				judgeTabItem.IsEnabled = false;
			else
			{
				makeJudge.IsEnabled = false;
				makeJudge.Visibility = Visibility.Collapsed;
				m_judge = judge;
			}

			// Get Organiser from database
			Organiser org = null;
			List<Organiser> orgs = DatabaseSingleton.GetDatabaseAccessor().QueryOrganiser(where, OrderBy.None, "");
			if (orgs.Count == 1)
				org = orgs[0];

			if (org == null)
				organiserTabItem.IsEnabled = false;
			else
			{
				makeOrganiser.IsEnabled = false;
				makeOrganiser.Visibility = Visibility.Collapsed;
				m_organiser = org;
			}
		}

		private void okButton_Click(object sender, RoutedEventArgs e)
		{
			if (!IsValid(this))
				return;

			try
			{
				// TODO: Start Transaction
				DatabaseSingleton.GetDatabaseAccessor().UpdateMember(m_member);
				DatabaseSingleton.GetDatabaseAccessor().UpdateCompetitor(m_competitor);
				DatabaseSingleton.GetDatabaseAccessor().UpdateJudge(m_judge);
				DatabaseSingleton.GetDatabaseAccessor().UpdateOrganiser(m_organiser);
				// TODO: Commit Transaction
			}
			catch
			{
				MessageBox.Show("Error saving to the database!");
				return;
			}

			this.DialogResult = true;
		}

		private void makeCompetitor_Click(object sender, RoutedEventArgs e)
		{
			Competitor comp = new Competitor();
			comp.MemberID = m_member.MemberID;
			CompetitorEditDialogBox cedb = new CompetitorEditDialogBox(comp);

			cedb.ShowDialog();

			if (cedb.DialogResult.HasValue && cedb.DialogResult.Value)
			{
				LoadGens();
			}
		}

		private void makeJudge_Click(object sender, RoutedEventArgs e)
		{
			Judge judge = new Judge();
			judge.MemberID = m_member.MemberID;
			JudgeEditDialogBox jedb = new JudgeEditDialogBox(judge);

			jedb.ShowDialog();

			if (jedb.DialogResult.HasValue && jedb.DialogResult.Value)
			{
				LoadGens();
			}
		}

		private void makeOrganiser_Click(object sender, RoutedEventArgs e)
		{
			Organiser org = new Organiser();
			org.MemberID = m_member.MemberID;
			OrganiserEditDialogBox oedb = new OrganiserEditDialogBox(org);

			oedb.ShowDialog();

			if (oedb.DialogResult.HasValue && oedb.DialogResult.Value)
			{
				LoadGens();
			}
		}

		bool IsValid(DependencyObject node)
		{
			// Check if dependency object was passed
			if (node != null)
			{
				// Check if the object is enabled
				if (node is UIElement)
				{
					// Disabled object doesn't need to be validated
					if (!((UIElement)node).IsEnabled)
						return true;
				}

				// Check if dependency object is valid.
				// NOTE: Validation.GetHasError works for controls that have validation rules attached 
				bool isValid = !Validation.GetHasError(node);
				if (!isValid)
				{
					// If the dependency object is invalid, and it can receive the focus,
					// set the focus
					if (node is IInputElement)
						Keyboard.Focus((IInputElement)node);
					return false;
				}
			}

			// If this dependency object is valid, check all child dependency objects
			foreach (object subnode in LogicalTreeHelper.GetChildren(node))
			{
				if (subnode is DependencyObject)
				{
					// If a child dependency object is invalid, return false immediately,
					// otherwise keep checking
					if (!IsValid((DependencyObject)subnode))
						return false;
				}
			}

			// All dependency objects are valid
			return true;
		}
	}
}
